package cn.interestingshop.dao.impl;

import cn.interestingshop.dao.BaseDao;
import cn.interestingshop.dao.BaseOrderDao;
import cn.interestingshop.entity.BaseOrder;
import cn.interestingshop.entity.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class BaseOrderDaoImpl extends BaseDao implements BaseOrderDao {
    @Override
    public List<BaseOrder> selectAll(int pageNo, int pageSize) {
        List<BaseOrder> list=new ArrayList<>();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            //1. 加载驱动
            //2. 获取连接对象
            conn = getConnection();
            //3. 定义sql语句
            String sql = "SELECT * FROM `interesting_shop_db`.`t_base_order` LIMIT ?, ?";
            //4. 创建执行对象
            ps = conn.prepareStatement(sql);
            //5. 填充参数
            ps.setInt(1,(pageNo-1)*pageSize);
            ps.setInt(2,pageSize);
            //6. 执行sql语句
            rs = ps.executeQuery();
            //7. 分析返回结果
            while (rs.next()) {
                BaseOrder order=new BaseOrder();
                order.setId(rs.getInt("id"));
                order.setUserId(rs.getInt("user_id"));
                order.setAccount(rs.getString("account"));
                order.setUserAddress(rs.getString("user_address"));
                order.setCreateTime(rs.getDate("create_time"));
                order.setAmount(rs.getFloat("amount"));
                order.setOrderNo(rs.getString("order_no"));

                list.add(order);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //8.释放资源关闭连接
            closeAll(rs,ps,conn);
        }
        return list;
    }

    @Override
    public int selectCount() {
        int count=0;
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            //1. 加载驱动
            //2. 获取连接对象
            conn = getConnection();
            //3. 定义sql语句
            String sql = "SELECT COUNT(1) FROM `interesting_shop_db`.`t_base_order`";
            //4. 创建执行对象
            ps = conn.prepareStatement(sql);
            //5. 填充参数
            //6. 执行sql语句
            rs = ps.executeQuery();
            //7. 分析返回结果
            while (rs.next()) {
                count=rs.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //8.释放资源关闭连接
            closeAll(rs,ps,conn);
        }
        return count;
    }

    @Override
    public int insert(BaseOrder baseOrder) {
        Connection conn=null;
        PreparedStatement ps=null;
        int rs=0;
        try {
            //1.加载驱动
            //2.创建连接
            conn = getConnection();
            //3.定义sql语句
            String sql="INSERT INTO `interesting_shop_db`.`t_base_order`(`user_id`, `account`, `user_address`, `create_time`, `amount`, `order_no`) " +
                    "VALUES (?, ?, ?, ?, ?, ?)";
            //4.创建执行对象
            ps=conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
            //5.填充参数
            ps.setObject(1,baseOrder.getUserId());
            ps.setObject(2,baseOrder.getAccount());
            ps.setObject(3,baseOrder.getUserAddress());
            ps.setObject(4,baseOrder.getCreateTime());
            ps.setObject(5,baseOrder.getAmount());
            ps.setObject(6,baseOrder.getOrderNo());
            //6.执行sql语句
            rs=ps.executeUpdate();
            //获取自动产生的id
            ResultSet generatedKeys = ps.getGeneratedKeys();
            if (generatedKeys.next()) {
                baseOrder.setId(generatedKeys.getInt(1));
            }
            //7.分析返回结果
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //8.释放资源，关闭连接
            closeAll(null,ps,conn);
        }
        return rs;
    }
}
